package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/2.
 */

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzWork;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 工作量控制层
 *
 * @author 甘汝雷
 * @create 2017-05-02 12:47
 **/
@Controller
@RequestMapping("/Work")
@SessionAttributes("userLogin")
public class XzWorkController {

    @Autowired
    private XzWorkService workService;
    @Autowired
    private XzEmpService empService;

    //添加工作量概况
    @RequestMapping("addWork.emp")
    public ModelAndView addWork(XzWork work) {
        ModelAndView mv = errors(work);
        if (mv.getViewName().equals("backEnd/workEdit"))return mv;
        try {
            workService.insert(work);
            XzWork work1 = new XzWork();
            work1.setEmpId(work.getEmpId());
            work1.setWorkWeekTime(work.getWorkWeekTime());
            List<XzWork> workList = workService.selAll(work1);
            mv.addObject("workList", workList);
        } catch (Exception e) {
            e.printStackTrace();
            mv.addObject("msg", "添加失败");
            mv.addObject("work", work);
            mv.setViewName("backEnd/workEdit");
        }
        mv.addObject("work", work);
        return mv;
    }
    //表单验证
    public ModelAndView errors(XzWork work){
        ModelAndView mv = new ModelAndView("redirect:selWorkAll.emp");
        Map errors = new HashMap();
        if (work.getWorkWeekTime()!=null){
            boolean b = workService.selDate(work);
            if (b)errors.put("workWeekTime","该日期已存在，不可重复创建");
        }else errors.put("workWeekTime","星期不能为空");
        if (work.getWorkPlan()==0.0)errors.put("workPlan","计划时间不能为空");
        if (errors.size()>0){
            mv.addObject("errors", errors);
            mv.setViewName("backEnd/workEdit");
            mv.addObject("work", work);
            return mv;
        }

        return mv;
    }

    //分页查询工作量概况
    @RequestMapping("selWorkAll.emp")
    public ModelAndView selWorkAll(XzWork work,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/workShow");
        if (work.getWorkWeekTime()==null){
            work.setWorkWeekTime(new Date());
        }
        if (work.getEmpId()==null)work.setEmpId(emp.getEmpId());
        List<XzWork> workList = workService.selAll(work);
        int plan = 0;
        int actual = 0;
        for ( XzWork w:workList) {
            plan += w.getWorkPlan();
            actual += w.getWorkActual();
        }
        Map whole = new HashMap();
        whole.put("plan",plan);
        whole.put("actual",actual);
        whole.put("deviation",actual-plan);
        if (plan!=0&&actual!=0)whole.put("rate",(actual-plan)*100/plan);
        mv.addObject("empList",empService.selAll(new XzEmp()));
        mv.addObject("whole",whole);
        mv.addObject("workList", workList);
        mv.addObject("work", work);
        return mv;
    }

    //编辑工作量
    @RequestMapping("workEdit.emp")
    public ModelAndView workEdit(@RequestParam(defaultValue = "0") Long workId, @ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/workEdit");
        if (workId != 0) {
            XzWork work = workService.selById(workId);
            mv.addObject("work", work);
        } else {
            XzWork work = new XzWork();
            work.setEmpId(emp.getEmpId());
            work.setWorkId((long)0);
            mv.addObject("work", work);
        }
        return mv;
    }
    //修改工作量
    @RequestMapping("updateWork.emp")
    public ModelAndView updateWork(XzWork work,@ModelAttribute("userLogin")XzEmp emp){
        ModelAndView mv = errors(work);
        if (mv.getViewName().equals("backEnd/workEdit"))return mv;
        if (work.getEmpId()==null)work.setEmpId(emp.getEmpId());
        try {
            workService.update(work,emp.getEmpRole());
            XzWork work1 = new XzWork();
            work1.setEmpId(work.getEmpId());
            work1.setWorkWeekTime(work.getWorkWeekTime());
            List<XzWork> workList = workService.selAll(work1);
            mv.addObject("workList",workList);
        }catch (Exception e){
            e.printStackTrace();
            mv.addObject("msg","修改失败");
            mv.addObject("work"+work);
            mv.setViewName("backEnd/workEdit");
        }
        mv.addObject("empList",empService.selAll(new XzEmp()));
        mv.addObject("work",work);
        return mv;
    }

    //删除工作量
    @RequestMapping("deleteWork.emp")
    public ModelAndView deleteWork(Long workId){
        ModelAndView mv =new ModelAndView("redirect:selWorkAll.emp");
        int i = workService.isdelete(workId);
        return mv;
    }

}
